package com.wenwan.manage.controller;

import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.wenwan.manage.service.UserService;
import com.wenwan.manage.setting.DefaultValue;

import javax.annotation.Resource;

/**
 * 用户
 * 
 * @author Admin
 *
 */
@Controller
@RequestMapping(value = "/user")
public class UserController {

	@Autowired
	public UserService userService;

	/**
	 * 创建用户
	 * 
	 * @param headimgurl
	 *            头像
	 * @param nickname
	 *            昵称
	 * @param autograph
	 *            签名
	 * @param zone
	 *            地区
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/create/or/update", method = { RequestMethod.POST })
	public Map<String, Object> createOrUpdate(@RequestParam(required = false) String headimgurl,
			@RequestParam(required = false) String nickname,
			@RequestParam(required = false, defaultValue = DefaultValue.AUTOGRAPH) String autograph,
			@RequestParam(required = false) String zone, @RequestParam(required = false) String openid) {
		return userService.createOrUpdate(headimgurl, nickname, autograph, zone, openid);
	}

	/**
	 * 删除
	 * 
	 * @param id
	 *            唯一标识
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/delete/by/id", method = { RequestMethod.POST })
	public Map<String, Object> deleteById(@RequestParam(required = true) String id) {
		return userService.deleteById(id);
	}

	/**
	 * 更新
	 * 
	 * @param id
	 *            唯一标识
	 * @param headimgurl
	 *            头像
	 * @param nickname
	 *            昵称
	 * @param autograph
	 *            签名
	 * @param zone
	 *            地区
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/update", method = { RequestMethod.POST })
	public Map<String, Object> update(@RequestParam(required = true) String id,
			@RequestParam(required = false) String headimgurl, @RequestParam(required = false) String nickname,
			@RequestParam(required = false) String autograph, @RequestParam(required = false) String zone,
			@RequestParam(required = false) String weiXin, @RequestParam(required = false) Integer releaseNumber,
			@RequestParam(required = false) String phone) {
		return userService.update(id, headimgurl, nickname, autograph, zone, weiXin, null, phone);
	}

	/**
	 * 查询全部
	 * 
	 * @param currentPage
	 *            当前页
	 * @param pageSize
	 *            每页显示条数
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/select/all", method = { RequestMethod.GET })
	public Map<String, Object> selectAll(
			@RequestParam(required = false, defaultValue = DefaultValue.CURRENT_PAGE) Integer currentPage,
			@RequestParam(required = false, defaultValue = DefaultValue.PAGE_SIZE) Integer pageSize) {
		return userService.selectAll(currentPage, pageSize);
	}

	/**
	 * 查询单个
	 * 
	 * @param id
	 *            唯一标识
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/select/one/by/id", method = { RequestMethod.GET })
	public Map<String, Object> selectOneById(@RequestParam(required = true) String id) {
		return userService.selectOneById(id);
	}

	/**
	 * 查询单个
	 * 
	 * @param openid
	 *            微信小程序唯一标识
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/select/one/by/openid", method = { RequestMethod.GET })
	public Map<String, Object> selectOneByOpenid(@RequestParam(required = true) String openid) {
		return userService.selectOneByOpenid(openid);
	}

}
